#coding: utf-8
import datetime
import json
from django.http import HttpResponse
from .models import RootCert,ChildCert
def index(request):
    return HttpResponse("You're voting on question .")

#获取子域名
def getCert(request):
    cert_count = ChildCert.objects.count()
    list1=[]
    for obj in ChildCert.objects.all():
        sub_cert_name = obj.sub_cert_name
        start_time = obj.start_time
        end_time = obj.end_time
        now_time = str(datetime.datetime.now())
        a3 = datetime.datetime.strptime(str(end_time)[0:10],"%Y-%m-%d")
        now_time = datetime.datetime.strptime(now_time[0:10],"%Y-%m-%d")
        expired_days = (a3 - now_time).days
        start_time = start_time.strftime('%Y-%m-%d ')
        print type(start_time)
        end_time = end_time.strftime('%Y-%m-%d ')
        remark = obj.remark

        dict = {'sub_cert_name': sub_cert_name, 'start_time': start_time, 'end_time': end_time,'remark':remark,'pk':obj.pk,'expired_days': expired_days}
        list1.append(dict)
    result = {"total":cert_count,'rows':list1}
    return HttpResponse(json.dumps(result))


#修改子域名
def modifyCert(request):
    ret = {'state': 200, 'message': "修改成功"}
    data = json.loads(request.body)
    data['start_time'] = str("%s%s" % (data['start_time'],'23:59:00.10'))
    data['end_time'] = str("%s%s" % (data['end_time'], '23:59:00.10'))
    try:
       number=data.pop('pk')
       ChildCert.objects.filter(id=number).update(**data)
    except Exception, e:
        print e
    return HttpResponse(json.dumps(ret), content_type="application/json")


#获取过期域名
def getExpiredCert(request):
    cert_count = ChildCert.objects.count()
    list1=[]
    for obj in ChildCert.objects.all():
        sub_cert_name = obj.sub_cert_name
        start_time = obj.start_time
        end_time = obj.end_time
        now_time = str(datetime.datetime.now())
        a3 = datetime.datetime.strptime(str(end_time)[0:10],"%Y-%m-%d")
        now_time = datetime.datetime.strptime(now_time[0:10],"%Y-%m-%d")
        expired_days = (a3 - now_time).days
        start_time = start_time.strftime('%Y-%m-%d ')
        print type(start_time)
        end_time = end_time.strftime('%Y-%m-%d ')
        remark = obj.remark


        if expired_days < 10:
            dict = {'sub_cert_name': sub_cert_name, 'expired_days': expired_days}
            print 'dsfsf'
            list1.append(dict)
    return HttpResponse(json.dumps(list1))


#添加子域名
def addCert(request):
    ret = {'state': 200, 'message': "添加成功"}
    data = json.loads(request.body)
    data['start_time'] = str("%s %s" % (data['start_time'],'23:59:00.10'))
    data['end_time'] = str("%s %s" % (data['end_time'], '23:59:00.10'))
    RootCert_id = data.pop('RootCert_id')
    obj = RootCert.objects.get(id = RootCert_id)
    data['RootCert_id'] = obj
    ChildCert.objects.create(**data)
    return HttpResponse(json.dumps(ret), content_type="application/json")


#获取根域名
def getRootDomain(request):
    cert_count = RootCert.objects.count()
    list1=[]
    for obj in RootCert.objects.all():
        main_cert_name = obj.main_cert_name
        remark = obj.remark
        dict = {'main_cert_name':main_cert_name,'remark':remark,'pk':obj.pk}
        list1.append(dict)
    result = {"total":cert_count,'rows':list1}
    return HttpResponse(json.dumps(result))


#获取根域名，主要用于下拉框
def getRootDomainList(request):
    result=[]
    for obj in RootCert.objects.all():
        main_cert_name = obj.main_cert_name
        remark = obj.remark
        dict = {'main_cert_name':main_cert_name,'remark':remark,'pk':obj.pk}
        result.append(dict)
    return HttpResponse(json.dumps(result))


#添加根域名
def addRootCert(request):
    ret = {'state': 200, 'message': "添加成功"}
    data = json.loads(request.body)
    RootCert.objects.create(**data)
    return HttpResponse(json.dumps(ret), content_type="application/json")


#修改根域名
def modifyRootCert(request):
    ret = {'state': 200, 'message': "修改成功"}
    data = json.loads(request.body)
    number = data.pop('pk')
    RootCert.objects.filter(id=number).update(**data)
    return HttpResponse(json.dumps(ret), content_type="application/json")



